package com.wave.wavesystem.serve.service.impl;

import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.wave.common.util.Func;
import com.wave.wavesystem.serve.mapper.CommentMapper;
import com.wave.wavesystem.serve.pojo.entity.Comment;
import com.wave.wavesystem.serve.pojo.query.CommentQuery;
import com.wave.wavesystem.serve.service.ICommentService;
import com.wave.wavesystem.serve.util.AuthUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;


@Service
@Slf4j
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements ICommentService {

    @Override
    public QueryChain<Comment> getQueryChain(CommentQuery query) {
        QueryChain<Comment> queryChain = queryChain();
        queryChain.orderBy(Comment::getLikecCount, false);
        queryChain.orderBy(Comment::getCreateTime, false);
        queryChain.eq(Comment::getStatus, 1);
        if (Func.isNotEmpty(query.getPostId())) {
            queryChain.eq(Comment::getPostId, query.getPostId());
        }
        Long userId = AuthUtil.userIdAsLong();
        if (Func.isNotEmpty(userId)) {
            queryChain.eq(Comment::getAuthorId, userId)
                    .isNull(Comment::getReplyTo);
        }
        log.info("CommentServiceImpl query: {}", queryChain.toSQL());
        return queryChain;
    }

}
